package com.loong.system.business;

import com.loong.common.mybatis.business.AbstractBusiness;
import com.loong.system.common.backend.param.SystemConfigureQueryParam;
import com.loong.system.model.SystemConfigure;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Repository
public class SystemConfigureBusiness extends AbstractBusiness<SystemConfigure> {
    /**
     * 分页查询
     * @param param
     * @return
     */
    public List<SystemConfigure> query(SystemConfigureQueryParam param){
        Condition condition = new Condition(SystemConfigure.class);
        Example.Criteria criteria = condition.createCriteria();
        buildQueryCriteria(param,criteria);
        return findByCondition(condition);
    }
    //这里根据业务需求构造条件
    private void buildQueryCriteria(SystemConfigureQueryParam param,Example.Criteria criteria){
        if(ObjectUtils.isNotEmpty(param.getPositionId())){
            criteria.andEqualTo("positionId", param.getPositionId());
        }
        if(ObjectUtils.isNotEmpty(param.getPositionCode())){
            criteria.andEqualTo("positionCode", param.getPositionCode());
        }
        if (ObjectUtils.isNotEmpty(param.getConfigName())) {
            criteria.andEqualTo("configName", param.getConfigName());
        }
        if (ObjectUtils.isNotEmpty(param.getActionType())) {
            // actionType 是 实体属性
            criteria.andEqualTo("actionType", param.getActionType());
        }
        if (ObjectUtils.isNotEmpty(param.getConfigType())) {
            // configType 是 实体属性
            criteria.andEqualTo("configType", param.getConfigType());
        }
        if(ObjectUtils.isNotEmpty(param.getConfigKey())){
            criteria.andEqualTo("configKey", param.getConfigKey());
        }
        if(ObjectUtils.isNotEmpty(param.getConfigStatus())){
            criteria.andEqualTo("configStatus", param.getConfigStatus());
        }
        /**
         * 查出有效时间内
         */
        if(ObjectUtils.isNotEmpty(param.getStartTime())){
            criteria.andLessThan("startTime",param.getStartTime());
        }
        if(ObjectUtils.isNotEmpty(param.getEndTime())){
            criteria.andGreaterThan("endTime",param.getEndTime());
        }
    }
}
